package com.loong.order.mq;

import com.loong.common.rabbitmq.dto.OrderDTO;
import com.loong.common.rabbitmq.dto.base.CommonEventDto;
import com.loong.common.rabbitmq.producer.ExpirationMessagePostProcessor;
import com.loong.common.utils.TransformUtils;
import com.loong.global.constant.MQConstant;
import com.loong.global.enums.mq.QueueEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-12-29 14:27
 */
@Slf4j
@Component
public class OrderReport {
    @Resource(name = "defaultRabbitTemplate")
    private RabbitTemplate defaultRabbitTemplate;

//    @Resource(name = "batchQueueRabbitTemplate")
//    private BatchingRabbitTemplate batchingRabbitTemplate;

//    @Resource
//    private CustomMessagePostProcessor customMessagePostProcessor;
    @Async
    public void report(CommonEventDto report) {
        try {
            log.debug("====进入上报阶段{}====上报信息为"+report.getData());
            //暂时测试Topic队列 是否都能监听到消息 后续改成订单
            report.setData(TransformUtils.transform(report.getData(), OrderDTO.class));
            defaultRabbitTemplate.convertAndSend(
                    //这里就用order_db的 es的也一样
                    MQConstant.BASE_PREFIX+ QueueEnums.ORDER_DB.getExchange()+MQConstant.BASE_SUFFIX.EXCHANGE,
                    MQConstant.BASE_PREFIX+QueueEnums.ORDER_DB.getRouterKey()+MQConstant.BASE_SUFFIX.TOPIC_MESSAGE,
                    report,
                    new ExpirationMessagePostProcessor(15_000L));
        } catch (Exception ex) {
            log.error("上报订单信息日志发生异常", ex);
        }
    }
}
